﻿#include "BindCommand.h"

#include "../../Modules/ModulesManager.h"

void BindCommand_onInit(ICommand* _this) {
	_this->aliasList.reserve(ModuleManager::NameTable.size());
	for (auto& i : ModuleManager::NameTable)
		_this->aliasList.emplace_back_unchecked(stringview_Object{std::u8string_view{i.first}, nullptr});
	
}

bool BindCommand_onExecute(const fast_io::vector<std::u8string>& args) {
	if (args.size() == 2) {
		auto modOpt{ModuleManager::getModuleByName(args[1])};
		if (modOpt == nullptr) 
			return false;
		char8_t key{static_cast<char8_t>(modOpt->getKeybind())};
		if (fast_io::char_category::is_c_alnum(key)) {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"Module ",
												 args[1],
												 u8" has been bound to the key: ",
												 fast_io::mnp::chvw(key));
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"模块 ",
												 args[1],
												 u8" 已经绑定到 ",
												 fast_io::mnp::chvw(key));
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"模塊 ",
												 args[1],
												 u8" 已經綁定到 ",
												 fast_io::mnp::chvw(key));
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"モジュール ",
												 args[1],
												 u8" はボタン ",
												 fast_io::mnp::chvw(key),
												 u8" をバインドしています！");
				break;
				std::unreachable();
			}
		} else {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"Module ",
												 args[1],
												 u8" has been bound to the key: ",
												 fast_io::mnp::hex0x(key));
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"模块 ",
												 args[1],
												 u8" 已经绑定到 ",
												 fast_io::mnp::hex0x(key));
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"模塊 ",
												 args[1],
												 u8" 已經綁定到 ",
												 fast_io::mnp::hex0x(key));
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"モジュール ",
												 args[1],
												 u8" はボタン ",
												 fast_io::mnp::hex0x(key),
												 u8" をバインドしています！");
				break;
				std::unreachable();
			}
		}
		return true;
	}
	if (args.size() != 3)
		return false;
	if (args[1].empty())
		return false;
	if (args[2].empty())
		return false;

	auto modOpt = ModuleManager::getModuleByName(args[1]);
	if (modOpt == nullptr) {
		switch (SettingManager::languageget) {
		case 0:
		default:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"Moudle ",
											 args[1],
											 u8" does not exist!");
			break;
		case 1:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"模块 ",
											 args[1],
											 u8" 不存在！");
			break;
		case 2:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"模塊 ",
											 args[1],
											 u8" 不存在！");
			break;
		case 3:
			C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
											 u8"[Helium] ",
											 Utils::TextColor::RED,
											 u8"モジュール ",
											 args[1],
											 u8" が存在しません！");
			break;
			std::unreachable();
		}
		return true;
	} else {
		char8_t key{};
		if (args[2].front() == u8'\\') {
			std::u8string arg2{args[2]};
			arg2.erase(args[2].begin());
			if (!scan<true>(fast_io::u8ibuffer_view(arg2), fast_io::mnp::hex_get(key)))
				return false;
			if (key == 0)
				return false;
			modOpt->setKeybind(key);
		} else {
			if (args[2].size() > 1)
				return false;
			key = args[2].front();
			if (!fast_io::char_category::is_c_alnum(key))
				return false;
			key = fast_io::char_category::to_c_upper(key);
			modOpt->setKeybind(key);
		}
		if (fast_io::char_category::is_c_alnum(key)) {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"Successfully bound module ",
												 args[1],
												 u8"to the key: ",
												 fast_io::mnp::chvw(key));
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"模块 ",
												 args[1],
												 u8" 成功绑定到： ",
												 fast_io::mnp::chvw(key));
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"模塊 ",
												 args[1],
												 u8" 成功綁定到： ",
												 fast_io::mnp::chvw(key));
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"モジュール ",
												 args[1],
												 u8" が ",
												 fast_io::mnp::chvw(key),
												 u8" に正常にバインドされました！");
				break;
				std::unreachable();
			}
		} else {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"Successfully bound module ",
												 args[1],
												 u8"to the key: ",
												 fast_io::mnp::hex0x(key));
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"模块 ",
												 args[1],
												 u8" 成功绑定到： ",
												 fast_io::mnp::hex0x(key));
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"模塊 ",
												 args[1],
												 u8" 成功綁定到： ",
												 fast_io::mnp::hex0x(key));
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"モジュール ",
												 args[1],
												 u8" が ",
												 fast_io::mnp::hex0x(key),
												 u8" に正常にバインドされました！");
				break;
				std::unreachable();
			}
		}
		return true;
	}
	return true;
}

ICommand BindCommand{u8"bind",
					 u8".b or .bind <module> <key (0-9, a-z, A-Z, \\1-\\FF (See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes for help))>",
					 {u8"Binds modules to specific keys", u8"将模块绑定到特定键", u8"將模塊綁定到特定鍵", u8"モジュールを特定のキーにバインドする"},
					 {u8"b"},
					 fast_io::vector<aliasList_struct>{},  // If empty must use fast_io::vector<aliasList_struct>{}
					 &BindCommand_onInit,
					 &BindCommand_onExecute};